import { ILoadMapTypeAll } from '@/common';
import { getPosIDs, getPosName, getTenantID, getUserID, isRevocationBtn } from '@/utils';
import { FormInstance } from 'antd';

const AllBtnsConfig = (
  curRes: Record<string, any>,
  detailInfo: Record<string, any>,
  nextBtnShow: boolean,
  contractForm: FormInstance,
  oldContractInfo: Record<string, any>,
  pageType: string,
) => {
  let isShowBtn = {
    allBtnDiv: false,
    saveBtn: false,
    revokeFlagBtn: false, //流程撤回按钮
    invalBoolBtn: false, //暂停开始按钮
    stopProcInstBtn: false, //流程作废接口
    applyBtnDiv: false, //提交按钮
    nextBtnDiv: false, //下一步按钮
    editBtnDiv: false, //编辑按钮
    signBtnDiv: false, //扫描件
    printBtnDiv: false, //打印
    modifyBtnDiv: false, //变更
    breakBtnDiv: false, //解除
    isShowCheckList: false, //是否显示流程记录
    revokeBtnDiv: false, //撤销按钮
    cancelBtnDiv: false, //作废
    recordFlagBtn: false, //补录

    flowHandList: {}, //流程办理数据
  };

  /** 科研助理可编辑判断 */
  const KYZL_PAND = (record) => {
    return (record?.createPositionID == getPosIDs() && record?.ownerID == getUserID()) || getPosName() == '科研助理';
    // return record?.createPositionID == getPosIDs() && record?.ownerID == getUserID();
  };

  /** 暂存按钮 */
  if (curRes['/contractController/add'] || curRes['/contractController/edit']) {
    if (detailInfo && detailInfo?.id) {
      if (
        (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000` ||
          (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1001`
            ? detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1001`
            : false)) &&
        !detailInfo?.cancelerID
      ) {
        isShowBtn['saveBtn'] = true;
        isShowBtn['allBtnDiv'] = true;
      }
    } else if (oldContractInfo?.changeWay == 0 || oldContractInfo?.changeWay == 1) {
      isShowBtn['saveBtn'] = true;
      isShowBtn['allBtnDiv'] = true;
    } else {
      isShowBtn['saveBtn'] = true;
      isShowBtn['allBtnDiv'] = true;
    }
  }

  /** 流程按钮 */
  if (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll?.CONTRACT_STATE}1001`) {
    isShowBtn['revokeFlagBtn'] = true;
    isShowBtn['stopProcInstBtn'] = true;
  }

  // 判断补录保存按钮
  if (detailInfo?.id) {
    if (
      curRes?.['/contractController/startProcess/{starterDeptID}'] &&
      detailInfo?.id &&
      detailInfo?.ownerID == getUserID() &&
      detailInfo?.createPositionID == getPosIDs() &&
      detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000`
    ) {
      isShowBtn['allBtnDiv'] = true;
      isShowBtn['recordFlagBtn'] = true;
    }
  } else {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['recordFlagBtn'] = true;
  }

  // 判断提交按钮
  if (
    curRes?.['/contractController/startProcess/{starterDeptID}'] &&
    detailInfo?.id &&
    detailInfo?.ownerID == getUserID() &&
    detailInfo?.createPositionID == getPosIDs() &&
    detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000`
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['applyBtnDiv'] = true;
  }

  // 判断提交按钮  未保存过的原合同依然显示提交按钮
  if (curRes?.['/contractController/startProcess/{starterDeptID}'] && !detailInfo?.id) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['applyBtnDiv'] = true;
  }

  // 下一步按钮
  if (nextBtnShow && pageType != 'detail') {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['nextBtnDiv'] = true;
  }

  // 编辑按钮
  if (
    curRes?.['/contractController/edit'] &&
    detailInfo?.id &&
    KYZL_PAND(detailInfo) &&
    pageType == 'detail' &&
    // detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000`
    (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1000` ||
      (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1001`
        ? detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1001`
        : false))
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['editBtnDiv'] = true;
  }

  // 扫描件
  if (
    curRes?.['/contractController/sign'] &&
    detailInfo?.id &&
    detailInfo?.ownerID == getUserID() &&
    detailInfo?.createPositionID == getPosIDs() &&
    detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1003` &&
    pageType == 'detail' &&
    (detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1002` ||
      detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003`)
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['signBtnDiv'] = true;
  }
  // 打印
  if (
    // curRes?.['/contractController/seal'] &&
    detailInfo?.id &&
    pageType == 'detail' &&
    KYZL_PAND(detailInfo) &&
    // detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1004`
    (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1003` ||
      detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1004`)
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['printBtnDiv'] = true;
  }

  // 变更
  if (
    curRes?.['/contractController/modifyRouter'] &&
    detailInfo?.id &&
    !detailInfo?.pID &&
    KYZL_PAND(detailInfo) &&
    detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1004` &&
    pageType == 'detail' &&
    (detailInfo?.relateNum == 0 ? true : detailInfo?.lockFlag == 1)
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['modifyBtnDiv'] = true;
  }

  // 解除
  if (
    pageType == 'detail' &&
    curRes?.['/contractController/breakRouter'] &&
    detailInfo?.id &&
    !detailInfo?.pID &&
    KYZL_PAND(detailInfo) &&
    detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1004` &&
    (detailInfo?.relateNum == 0 ? true : detailInfo?.lockFlag == 1)
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['breakBtnDiv'] = true;
  }

  // // 作废
  // if (
  //   curRes['/contractController/cancel'] &&
  //   detailInfo?.id &&
  //   detailInfo?.ownerID == getUserID() &&
  //   (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1002` ||
  //     detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1003` ||
  //     (!detailInfo?.pID ? false : detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1004`)) &&
  //   (detailInfo?.relateNum == 0 ? true : detailInfo?.lockFlag == 1)
  // ) {
  //   isShowBtn['allBtnDiv'] = true;
  //   isShowBtn['cancelBtnDiv'] = true;
  // }

  // 作废申请
  if (
    detailInfo?.id &&
    detailInfo?.ownerID == getUserID() &&
    detailInfo?.createPositionID == getPosIDs() &&
    detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1003` &&
    (detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1002` ||
      detailInfo?.checkState == `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}1003`)
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['cancelBtnDiv'] = true;
  }

  // 撤销
  if (
    curRes['/contractController/invalid'] &&
    detailInfo?.id &&
    detailInfo?.ownerID == getUserID() &&
    detailInfo?.createPositionID == getPosIDs() &&
    detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CONTRACT_STATE}1001`
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['revokeBtnDiv'] = true;
  }

  /** 处理流程办理数据 */
  const taskType: string = `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}1007`;
  const taskHandInfo: Record<string, any> = detailInfo?.paramObject?.[taskType];
  if (
    taskHandInfo &&
    taskHandInfo?.mactivitiTaskList?.length > 0 &&
    taskHandInfo?.mactivitiTaskList?.[0]?.assigneePositionID == getPosIDs() &&
    taskHandInfo?.mactivitiTaskList?.[0]?.assigneeID == getUserID()
  ) {
    isShowBtn['invalBoolBtn'] = true;
    isShowBtn['allBtnDiv'] = true;
    // isShowBtn['flowHandList'] = taskHandInfo?.mactivitiTaskList?.[0];
  }
  isShowBtn['flowHandList'] = detailInfo;

  //是否显示流程记录
  if (taskHandInfo && JSON.stringify(taskHandInfo) != '{}') {
    isShowBtn['isShowCheckList'] = true;
  }

  if (isRevocationBtn(detailInfo, taskType)) {
    isShowBtn['allBtnDiv'] = true;
  }

  return isShowBtn;
};

export { AllBtnsConfig };
